package com.yanxu;

import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.remoting.protocol.heartbeat.MessageModel;

import java.util.List;

/**
 * @author 折戟沉沙铁未销
 * @version V1.0
 * @date 2024/5/22-2024
 * @Description: 消费模式
 */
public class ModelConsumer {
    public static void main(String[] args) throws Exception {
        //定义消费者
        DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("consumer-model");
        //DefaultMQPullConsumer defaultMQPullConsumer = new DefaultMQPullConsumer();

        consumer.setNamesrvAddr("139.129.91.21:9876");
        consumer.setMessageModel(MessageModel.CLUSTERING);  //定义消费模式为  【集群消费模式】。 默认也是该消费模式
        consumer.subscribe("test","*");

        consumer.registerMessageListener(new MessageListenerConcurrently() {
            @Override
            public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgs, ConsumeConcurrentlyContext context) {
                for (MessageExt msg : msgs) {
                    System.out.println(msg.getTopic());
                    System.out.println(new String(msg.getBody()));
                }
                return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
            }
        });

        consumer.start();;

    }
}